<%@ page contentType="text/html;charset=UTF-8" language="java" %>

    <% String path=request.getContextPath(); String basePath=request.getScheme() + "://" + request.getServerName() + ":"
        + request.getServerPort() + path + "/" ; %>
        <!DOCTYPE html>

        <head>
            <base href="<%=basePath%>">
            <title>订单创建</title>
            <meta http-equiv="pragma" content="no-cache">
            <meta http-equiv="cache-control" content="no-cache">
            <meta http-equiv="expires" content="0">
            <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
            <meta http-equiv="description" content="This is my page">
        </head>

        <body>
            <style>
                .tdText {
                    text-align: right;
                    width: 400px;
                    height: 30px;
                }
            </style>
        </body>
        <%@ include file="/WEB-INF/jsp/common/header.jsp" %>
            <%@ include file="/WEB-INF/jsp/common/loadDiv.jsp" %>

                <div class="query_criteria" id="query_criteria">
                    <div style="margin: 5px 0px">
                        <%-- 搜索框 --%>
                            <label style="font-size: 12px;">创建时间：</label> <input class="easyui-datebox" name="startTime"
                                id="startTime"
                                data-options="required:true,showSeconds:false,formatter:myformatter,parser:myparser"
                                value="" style="width: 150px"> <label style="font-size: 10px;">~</label> <input
                                class="easyui-datebox" name="endTime" id="endTime"
                                data-options="required:true,showSeconds:false,formatter:myformatter,parser:myparser"
                                style="width: 150px" /> <label style="margin-left: 20px; font-size: 12px;">状态：</label>
                            <input class="easyui-combobox" id="status" name="status" style="width: 80px;" data-options="valueField: 'value',editable:false,
		textField: 'label',panelHeight:'auto',data: [{
			label: '全部',
			value: ''
		},{
			label: '待提交',
			value: '0',
			selected:true
		},{
			label: '已提交',
			value: '1'
		},{
			label: '已完成',
			value: '2'
		}]">
                            <a id="search" style="margin-left: 30px" href="javascript:void(0)" class="easyui-linkbutton"
                                data-options="iconCls:'icon-search'" onclick="selectList()">查询</a>
                            <a id="export" href="javascript:void(0)" class="easyui-linkbutton"
                                data-options="iconCls:'icon-export'" onclick="exportOrderList()">导出</a>
                    </div>
                    <table id="dg"></table>
                    <div id="orderWindow" class="easyui-window" closed="true" modal="true" title="订单详情"
                        style="width: 900px; height: 400px; padding: 5px 5px">
                        <form id="supplyOfferForm">
                            <div class="easyui-tabs" id="tabs" style="width: 875px; height: 350px">
                                <div title="基本信息" style="padding: 5px;">
                                    <table>
                                        <tr>
                                            <td><label>订单编码：</label> </td>
                                            <td><input id="ordCode" class="easyui-textbox"
                                                    data-options="disabled:true" /></td>
                                            <td><label>订单名称：</label> </td>
                                            <td>
                                                <input id="ordName" class="easyui-textbox"
                                                    data-options="required:true" />
                                                <input id="resId" type="hidden" />
                                                <input id="ordId" type="hidden" />
                                            </td>
                                            <td><label>费用：</label> </td>
                                            <td>
                                                <input id="ordFreight" class="easyui-textbox"
                                                    data-options="disabled:true" />
                                            </td>
                                        </tr>
                                        <tr>
                                            <td><label>付款条件：</label> </td>
                                            <td>
                                                <input class="easyui-combobox" id="cobPayment" name="cobPayment"
                                                    data-options="required:true,editable:false, url:'sysDict/queryItemByTypeName?dicName=付款条件',
                                                    method: 'get',
                                                    valueField: 'dicValue',
                                                    textField: 'dicName'">
                                            </td>
                                            <td><label>结算标识：</label> </td>
                                            <td>
                                                <input class="easyui-combobox" id="inqIsSettle" name="inqIsSettle"
                                                    data-options="required:true,valueField: 'value',editable:false,
    textField: 'label',panelHeight:'auto',data: [{
        label: '按原发数结算',
        value: '1',
        selected:true
    },{
        label: '按实收数结算',
        value: '2'
    }]">
                                            </td>
                                            <td><label>是否推送OA：</label> </td>
                                            <td>
                                                <input id="ordContractType" class="easyui-combobox" data-options="required:true,valueField: 'value',editable:false,
textField: 'label',panelHeight:'auto',data: [{
    label: '是',
    value: '1'
},{
    label: '否',
    value: '0'
}]">
                                        </tr>
                                        <tr>
                                            <td>是否自提：</label> </td>
                                            <td>
                                                <input class="easyui-combobox" id="inqIsOwn" name="inqIsOwn"
                                                    data-options="required:true,valueField: 'value',editable:false,
    textField: 'label',panelHeight:'auto',data: [{
        label: '供应商送货',
        value: '2',
        selected:true
    },{
        label: '自提',
        value: '1'
    },{
        label: '代加工自提',
        value: '70'
    }]">
                                            </td>
                                            <td><label>是否过磅
                                                    ：</label> </td>
                                            <td> <input class="easyui-combobox" id="inqIsWeight" name="inqIsWeight"
                                                    data-options="required:true,valueField: 'value',editable:false,
		textField: 'label',panelHeight:'auto',data: [{
			label: '无需过磅',
			value: '1'
		},{
			label: '需过磅',
			value: '2',
			selected:true
		}]">
                                            </td>
                                            <td><label>币种
                                                    ：</label> </td>
                                            <td><input class="easyui-combobox" id="inqCurrencyCode"
                                                    name="inqCurrencyCode" data-options="required:true,valueField: 'value',editable:false,
		textField: 'label',panelHeight:'auto', url: 'sysDict/queryItemByTypeName?dicName=币种',
        method: 'get',
        valueField: 'dicValue',
        textField: 'dicName'">
                                            </td>
                                        </tr>
                                        <tr>
                                            <td><label>合同起始日期：</label> </td>
                                            <td> <input data-options="required:true,editable:false"
                                                    id="inqContractStart" class="easyui-datebox"></td>
                                            <td><label>结束日期：</label> </td>
                                            <td>
                                                <input data-options="required:true,editable:false" id="inqContractEnd"
                                                    class="easyui-datebox">
                                            </td>
                                            <td><label>付款方式
                                                    ：</label> </td>
                                            <td> <input class="easyui-combobox" id="cobPayMode" name="cobPayMode"
                                                    data-options="required:true,valueField: 'value',editable:false,
		textField: 'label',panelHeight:'auto', url: 'sysDict/queryItemByTypeName?dicName=OA付款方式',
        method: 'get',
        valueField: 'dicValue',
        textField: 'dicName'">
                                            </td>
                                        </tr>
                                        <tr>
                                            <td>
                                                <label>备注&nbsp;&nbsp;&nbsp;&nbsp;：</label>
                                            </td>
                                            <td colspan="4"> <input id="remark" style="width: 300px;height:150px"
                                                    class="easyui-textbox" data-options="readonly:false,multiline:true">
                                            </td>
                                        </tr>
                                    </table>

                                </div>
                                <div title="物资明细" style="padding: 5px;">
                                    <table id="planMmDetailList"></table>
                                </div>
                                <div title="供应商明细" style="padding: 5px;">
                                    <table id="suppliesList"></table>
                                </div>
                                <div title="附件" style="padding: 5px;">
                                    <table>
                                        <tr>
                                            <td colspan="3"><input type="button" id="uploadbtn" value="选择文件..." />
                                                <input type="file" id="uploadpicker" accept="" onchange="changeFile()"
                                                    multiple style="display: none;" />
                                                <span>（文件名称不能包含斜杠、空格、&、#等特殊字符）</span>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td><label style="width: 40%; text-align: right;">附件下载：</label>
                                                <ul id="checkfileList"></ul>
                                            </td>
                                        </tr>
                                    </table>
                                </div>
                            </div>
                        </form>
                    </div>

                    <%-- 选择计划物资明细的弹窗 --%>
                        <div id="selectMMWindow" class="easyui-window" closed="true" modal="true" title="采购计划"
                            style="width: 900px; height: 400px; padding: 5px 5px">
                            <div style="margin: 5px 0px">
                                <%-- 搜索框 --%>
                                    <label style="font-size: 12px;">创建时间：</label> <input class="easyui-datebox"
                                        name="startMMTime" id="startMMTime"
                                        data-options="required:true,showSeconds:false,formatter:myformatter,parser:myparser"
                                        value="" style="width: 150px"> <label style="font-size: 10px;">~</label> <input
                                        class="easyui-datebox" name="endMMTime" id="endMMTime"
                                        data-options="required:true,showSeconds:false,formatter:myformatter,parser:myparser"
                                        style="width: 150px" /> </select> <a id="search" style="margin-left: 30px"
                                        href="javascript:void(0)" class="easyui-linkbutton"
                                        data-options="iconCls:'icon-search'" onclick="selectMMListByParams()">查询</a><a
                                        id="search" style="margin-left: 5px" href="javascript:void(0)"
                                        class="easyui-linkbutton" data-options="iconCls:'icon-add'"
                                        onclick="addRowsToPlanMmDetailList()">加入</a>
                            </div>
                            <%--计划物资列表DataGrid --%>
                                <table id="mmDataGrid"></table>
                        </div>

                        <%-- 选择供应商的弹窗 --%>
                            <div id="selectSupplyWindow" class="easyui-window" closed="true" modal="true" title="待选供应商"
                                style="width: 900px; height: 400px; padding: 5px 5px">
                                <%--待选供应商列表DataGrid --%>
                                    <table id="supplyDataGrid"></table>
                            </div>


                </div>

                <script type="text/javascript">
                    $(function () {
                        $('#startTime').datebox('setValue', getBeforeCurrentDate30Days());
                        $('#endTime').datebox('setValue', getNowFormatDate());
                        bindGrid();
                        selectMMGrid();
                        // bindMMCombo();
                        suppliesDatagird();
                        $("#uploadbtn").bind("click", function (e) {
                            $("#uploadpicker").click();
                        });
                    });

                </script>
                <script>
                    // 库存地点
                    function initStock(newValue) {
                        $('#inqStock').combobox({
                            method: 'get',
                            url: 'sysDict/queryItemByTypeName?dicName=' + newValue + '-库存地点',
                            valueField: "dicValue",
                            textField: "dicName",
                            editable: false,
                            required: true
                        });
                    }

                    var planTransferOptions = [{
                        "value": "集采",
                        "text": "集采"
                    }, {
                        "value": "自采",
                        "text": "自采"
                    }];

                </script>
                <script>
                    /**
                     * 条件查询待选供应商的方法
                     */
                    function selectSupplyListByParams() {
                        let supplyDataGrid = $('#supplyDataGrid');
                        supplyDataGrid.datagrid('load', {
                        });
                        supplyDataGrid.datagrid('enableFilter');
                    }

                    /**
                     * 构建待选择的供应商列表window
                     */
                    function setSuppliesDatagirdBySelection() {
                        $('#supplyDataGrid').datagrid('clearChecked');
                        selectSupplyListByParams()
                        $('#selectSupplyWindow').window('open');

                    }

                    // 构建供应商明细dataGrid
                    function setSuppliesDatagird(data, action) {
                        var supplyToolBar = [];
                        if (action == 'submit') {
                            supplyToolBar = [];
                        } else {
                            // 供应商列表的按钮工具栏
                            supplyToolBar = [{
                                text: '新增',
                                iconCls: 'icon-add',
                                handler: function () {
                                    setSuppliesDatagirdBySelection();
                                }
                            }, '-', {
                                text: '移除',
                                iconCls: 'icon-remove',
                                handler: function () {
                                    $.messager.confirm("提示", "确认移除?", function (e) {
                                        if (e) {
                                            removeMm('supply')
                                        }
                                    })
                                }
                            }];
                        }
                        let suppliesList = $('#suppliesList')
                        suppliesList.datagrid({
                            fitColumns: true,
                            data: data,
                            iconCls: 'icon-application',
                            remoteFilter: true,
                            clientPaging: false,
                            filterDelay: 0,
                            width: 860,
                            height: 300,
                            nowrap: true,
                            rownumbers: true,
                            animate: false,
                            collapsible: false,
                            singleSelect: true,
                            frozenColumns: [[{
                                field: 'infId',
                                title: '供应商id',
                                hidden: true,
                                sortable: true,
                                formatter: function (val, rec) {
                                    return unescape(val ? val : '')
                                }
                            },
                            {
                                field: 'gramId',
                                title: '方案id',
                                hidden: true,
                                sortable: true,
                                width: 100,
                                formatter: function (val, rec) {
                                    return unescape(val ? val : '')
                                }
                            },
                            {
                                field: 'infCode',
                                title: '供应商编码',
                                sortable: true,
                                width: 100,
                                formatter: function (val, rec, index) {
                                    return unescape(val ? val : '')
                                }
                            }, {
                                field: 'infName',
                                title: '名称',
                                sortable: true,
                                width: 300,
                                formatter: function (val, rec) {
                                    return unescape(val ? val : '')
                                }
                            }]],
                            // columns: [
                            //     [
                            // {
                            //     field: 'infCreditCode',
                            //     title: '社会信用代码',
                            //     sortable: true,
                            //     formatter: function (val, rec) {
                            //         return val ? unescape(val) : '';
                            //     }
                            // }, {
                            //     field: 'infCountry',
                            //     title: '国家',
                            //     sortable: true,
                            //     formatter: function (val, rec) {
                            //         return val ? unescape(val) : '';
                            //     }
                            // }, {
                            //     field: 'infProvince',
                            //     title: '地区',
                            //     sortable: true,
                            //     formatter: function (val, rec) {
                            //         return val ? unescape(val) : '';
                            //     }
                            // }, {
                            //     field: 'infCity',
                            //     title: '地市',
                            //     sortable: true,
                            //     formatter: function (val, rec) {
                            //         return val ? unescape(val) : '';
                            //     }
                            // }, {
                            //     field: 'infAddress',
                            //     title: '单位地址',
                            //     sortable: true,
                            //     formatter: function (val, rec) {
                            //         return val ? unescape(val) : '';
                            //     }
                            // }, {
                            //     field: 'infMobile',
                            //     title: '手机',
                            //     sortable: true,
                            //     formatter: function (val, rec) {
                            //         return val ? unescape(val) : '';
                            //     }
                            // }, {
                            //     field: 'infContactsName',
                            //     title: '联系人姓名',
                            //     sortable: true,
                            //     formatter: function (val, rec) {
                            //         return val ? unescape(val) : '';
                            //     }
                            // }
                            //     ]
                            // ],
                            toolbar: supplyToolBar,
                            onEndEdit: function (index, row) { },
                            onLoadSuccess: function () {

                            }
                        });

                    }

                    function suppliesDatagird() {
                        let supplyDataGrid = $('#supplyDataGrid');
                        supplyDataGrid.datagrid({
                            queryParams: {
                            },
                            fitColumns: true,
                            url: 'supply/info/selectList',
                            iconCls: 'icon-application',
                            clientPaging: false,
                            checkOnSelect: true,
                            remoteFilter: true,
                            filterDelay: 0,
                            width: 860,
                            height: 300,
                            nowrap: true,
                            rownumbers: true,
                            animate: false,
                            collapsible: false,
                            singleSelect: true,
                            pageSize: 6,
                            pageList: [6, 18, 28, 50],
                            remoteSort: true,
                            pagination: true,
                            frozenColumns: [[{
                                field: 'ck',
                                checkbox: true,
                                width: 100,
                            },
                            {
                                field: 'infId',
                                title: '供应商id',
                                hidden: true,
                                sortable: true,
                                width: 100,
                                formatter: function (val, rec) {
                                    return unescape(val ? val : '')
                                }
                            },
                            {
                                field: 'gramId',
                                title: '方案id',
                                hidden: true,
                                sortable: true,
                                width: 100,
                                formatter: function (val, rec) {
                                    return unescape(val ? val : '')
                                }
                            },
                            {
                                field: 'infCode',
                                title: '供应商编码',
                                sortable: true,
                                formatter: function (val, rec, index) {
                                    return unescape(val ? val : '')
                                }
                            }, {
                                field: 'infName',
                                title: '名称',
                                sortable: true,
                                formatter: function (val, rec) {
                                    return unescape(val ? val : '')
                                }
                            },]],
                            columns: [
                                [
                                    {
                                        field: 'infCreditCode',
                                        title: '社会信用代码',
                                        sortable: true,
                                        formatter: function (val, rec) {
                                            return val ? unescape(val) : '';
                                        }
                                    }, {
                                        field: 'infCountry',
                                        title: '国家',
                                        sortable: true,
                                        formatter: function (val, rec) {
                                            return val ? unescape(val) : '';
                                        }
                                    }, {
                                        field: 'infProvince',
                                        title: '地区',
                                        sortable: true,
                                        formatter: function (val, rec) {
                                            return val ? unescape(val) : '';
                                        }
                                    }, {
                                        field: 'infCity',
                                        title: '地市',
                                        sortable: true,
                                        formatter: function (val, rec) {
                                            return val ? unescape(val) : '';
                                        }
                                    }, {
                                        field: 'infAddress',
                                        title: '单位地址',
                                        sortable: true,
                                        formatter: function (val, rec) {
                                            return val ? unescape(val) : '';
                                        }
                                    }, {
                                        field: 'infMobile',
                                        title: '手机',
                                        sortable: true,
                                        formatter: function (val, rec) {
                                            return val ? unescape(val) : '';
                                        }
                                    }, {
                                        field: 'infContactsName',
                                        title: '联系人姓名',
                                        sortable: true,
                                        formatter: function (val, rec) {
                                            return val ? unescape(val) : '';
                                        }
                                    }
                                ]
                            ],
                            toolbar: [{
                                text: '添加',
                                iconCls: 'icon-ok',
                                handler: function () {
                                    addRowsToProgramSupplyDetailList();
                                }
                            }],
                            onLoadSuccess: function () { },
                        })
                        supplyDataGrid.datagrid('enableFilter');
                    }

                    /**
                     * 将选中的供应商添加到供应商列表
                     */
                    function addRowsToProgramSupplyDetailList() {
                        let suppliesList = $('#supplyDataGrid');
                        const checkedRows = suppliesList.datagrid('getChecked');
                        if (!checkedRows || checkedRows.length < 1) {
                            $.messager.alert('提示', '请至少选择一条数据');
                            return
                        }
                        const planMmrows = $('#suppliesList').datagrid('getRows')
                        if (checkedRows.length > 0) {
                            for (let i in checkedRows) {
                                if (!checkedRows[i].infCode) {
                                    $.messager.alert('消息', '供应商未审核！');
                                    return;
                                }
                                const result = planMmrows.some((item, index, arr) => {
                                    console.log('item:', item)
                                    console.log('checkedRows:', checkedRows[i].infId)
                                    return item.infId == checkedRows[i].infId
                                })
                                if (!result) {
                                    $('#suppliesList').datagrid('insertRow', {
                                        row: checkedRows[i]
                                    })
                                }
                                else {
                                    $.messager.alert('提示', '供应商已存在');
                                    return
                                }
                            }
                        }
                        $('#selectSupplyWindow').window('close');
                    }
                </script>

                <script>
                    var selectedFileList = [];//已选文件列表

                    /**
                     * 保存附件
                     */

                    function saveAttch(row, obj, action) {
                        obj.ordId = row
                        let saveNum = 0
                        if (selectedFileList.length > 0) {
                            $.each(selectedFileList, function (index, item) {
                                showFilediv();
                                var dataform = new FormData();
                                dataform.append("file", item);
                                var xmlhttp = null;
                                if (window.XMLHttpRequest) {// code for all new browsers
                                    xmlhttp = new XMLHttpRequest();
                                } else if (window.ActiveXObject) {// code for IE5 and IE6
                                    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
                                }
                                if (xmlhttp != null) {
                                    xmlhttp.open("POST", "api/file/springUpload?fileRow=" + row
                                        + "&fileTable=purchase_order&fileName="
                                        + item.name, true);
                                    xmlhttp.setRequestHeader('token', localStorage.getItem('token'));
                                    xmlhttp.setRequestHeader('userType', localStorage.getItem('userType'));
                                    xmlhttp.send(dataform);
                                    xmlhttp.onreadystatechange = callbackMethods;
                                } else {
                                    alert("你的浏览器版本不兼容，请使用更高版本的浏览器");
                                }

                                function callbackMethods() {
                                    if (xmlhttp.readyState == 4) {
                                        if (xmlhttp.status == 200) {
                                            saveNum += 1
                                            if (saveNum == selectedFileList.length) {
                                                hideFilediv();
                                                if ('submit' == action) {
                                                    submitData(obj)
                                                }
                                            }
                                        }
                                    } else {
                                        if (xmlhttp.status == 500) {
                                        }
                                    }
                                }
                            });
                        } else {
                            if ('submit' == action) {
                                submitData(obj)
                            }
                        }
                    }
                    /************文件处理部分*************/
                    /**
                     *文件框改变事件
                     */
                    function changeFile() {
                        var files = $("#uploadpicker").prop("files");
                        $.each(
                            files,
                            function (index, item) {
                                var choiseFile = $("#checkfileList>li");
                                if (choiseFile.length > 0) {//文件去重
                                    var count = 0;
                                    $.each(choiseFile, function (index1, item1) {
                                        if (item.name == item1.innerText) {
                                            count++;
                                        }
                                    });
                                    if (count == 0) {
                                        if (checkFileLength(item.size)) {
                                            if (item.name.includes('#') || item.name.includes('/') || item.name.includes('&') || item.name.includes('\\') || item.name.includes(' ')) {
                                                alert('文件名不能包含&、斜杠、反斜杠、空格等特殊字符！');
                                                return;
                                            }
                                            $("#checkfileList")
                                                .append(
                                                    "<li class=\"fileinfo\">"
                                                    + item.name
                                                    + "<a style=\"display: none; color: red; margin-left: 10px;\" href=\"javascript:void(0)\" onclick=\"RemoveFile(this,'"
                                                    + item.name
                                                    + "')\">删除</a>"
                                                    + "</li>");
                                            selectedFileList.push(item);
                                            AddMoushover();
                                        }
                                    }
                                } else {
                                    if (checkFileLength(item.size)) {
                                        if (item.name.includes('#') || item.name.includes('/') || item.name.includes('&') || item.name.includes('\\') || item.name.includes(' ')) {
                                            alert('文件名不能包含&、斜杠、反斜杠、空格等特殊字符！');
                                            return;
                                        }
                                        $("#checkfileList")
                                            .append(
                                                "<li class=\"fileinfo\">"
                                                + item.name
                                                + "<a style=\"display: none; color: red; margin-left: 10px;\" href=\"javascript:void(0)\" onclick=\"RemoveFile(this,'"
                                                + item.name
                                                + "')\">删除</a>"
                                                + "</li>");
                                        selectedFileList.push(item);
                                        // 										sselectedFileNameList.push(item.name);
                                        AddMoushover();
                                    }
                                }
                            });
                    }

                    /**
                     *    判断文件是否超过限制大小
                     * @param fileLen
                     * @returns {boolean}
                     */
                    function checkFileLength(fileLen) {
                        if (fileLen > 102400000) {
                            $.messager.alert("提示", "上传文件大小不能超过100M", "error");
                            return false;
                        }
                        return true;
                    }

                    /**
                     * //添加鼠标经过事件，鼠标划过时显示“删除”
                     * @constructor
                     */
                    function AddMoushover() {
                        $(".fileinfo").hover(function () {
                            $(this).find("a").last().css("display", "");
                        }, function () {
                            $(this).find("a").last().css("display", "none");
                        });
                    }

                    /**
                     *  移除已选择的文件
                     */

                    function RemoveFile(obj, fileId) {

                        $.messager.confirm("提示", "确认删除当前文件?", function (e) {
                            if (e) {
                                let index = $("#checkfileList>li").index($(obj).parent());
                                selectedFileList.splice(index, 1);
                                $(obj).parent().remove();
                                // 					var index1 = $.inArray(filename, sselectedFileNameList);
                                // 					sselectedFileNameList.splice(index1, 1);
                                $.ajax({
                                    type: "post",
                                    url: 'file/deleteFile?fileId=' + fileId,
                                    success: function (data) {
                                        $.messager.alert('消息', data);
                                    },
                                    error: function (XMLHttpRequest, textStatus,
                                        errorThrown) {
                                        $.messager.alert('消息', errorThrown);
                                    }
                                });
                            }
                        });
                    }

                    /**
                     * 取得该条数据所属的文件列表
                     * @param row
                     */
                    function getFileList(row) {
                        $.ajax({
                            type: "post",
                            url: "file/getFileList",
                            data: {
                                fileTable: "purchase_order",
                                fileRow: row.ordId
                            },
                            success: function (data) {
                                const dataArray = eval(data);
                                initFile(dataArray, row.status);
                            },
                            error: function (XMLHttpRequest, textStatus, errorThrown) {
                                $.messager.alert('消息', errorThrown);
                            }
                        });
                    }

                    /**
                     * 初始化该条数据的文件列表
                     * @param dataArray
                     */
                    function initFile(dataArray, status) {
                        $("#checkfileList").empty();
                        // 			sselectedFileNameList = [];
                        selectedFileList = [];
                        dataArray
                            .forEach(function (e, i, a) {
                                $("#checkfileList")
                                    .append(
                                        "<li class=\"fileinfo\">"
                                        + "<a style=\" color: blue; margin-left: 30px;\" href=\"javascript:void(0)\" onclick=\"downloadFile('" + e.FILE_NAME + "','"
                                        + e.FILE_ID
                                        + "')\">"
                                        + e.FILE_NAME
                                        + "</a>"
                                        + (status > 0 ? '' : "<a style=\"display: none; color: red; margin-left: 30px;\" href=\"javascript:void(0)\" onclick=\"RemoveFile(this,'"
                                            + e.FILE_ID + "')\">删除</a>")
                                        + "</li>");
                                // 						sselectedFileNameList.push(e.FILE_NAME);
                                if (status > 0) {
                                } else {
                                    AddMoushover();
                                }
                            });
                    }

                    function downloadFile(fileName, fileRow) {
                        let url = 'api/file/download?id=' + fileRow
                        let xhr = new XMLHttpRequest()
                        xhr.open('GET', url, true)
                        xhr.setRequestHeader('token', localStorage.getItem('token'));
                        xhr.setRequestHeader('userType', localStorage.getItem('userType'));
                        xhr.responseType = 'blob'
                        xhr.onload = function (data) {
                            if (this.status == 200) {
                                let blob = this.response
                                let reader = new FileReader()
                                reader.readAsDataURL(blob)
                                reader.onload = function (e) {
                                    let a = document.createElement('a')
                                    a.download = fileName
                                    a.href = e.target.result
                                    $('body').append(a)
                                    a.click()
                                    $(a).remove()
                                }
                            } else {

                            }
                        }
                        xhr.send()
                    }

                </script>

                <script>
                    var editIndex = undefined;

                    // 行编辑结束
                    function endEditing() {
                        if (editIndex == undefined) {
                            return true
                        }
                        if ($('#planMmDetailList').datagrid('validateRow', editIndex)) {
                            $('#planMmDetailList').datagrid('endEdit', editIndex);
                            editIndex = undefined;
                            return true;
                        } else {
                            alert('请先完成本行必填数据输入');
                            return false;
                        }
                    }
                    function cellStyler(value, row, index) {
                        return 'background-color:#ffee00;color:red;';
                    }

                    // 根据交货工厂，获取库存地点的下拉数据
                    function getRepertoryAddress(value) {
                        return 'sysDict/queryItemByTypeName?dicName=' + value + '-库存地点'
                    }

                    var inqIsFreeOptions = [{
                        "inqIsFree": "1",
                        "text": "付费"
                    }, {
                        "inqIsFree": "X",
                        "text": "免费"
                    }];
                    var inqBackGoodOptions = [{
                        "inqBackGood": "1",
                        "text": "采购"
                    }, {
                        "inqBackGood": "X",
                        "text": "退货"
                    }];
                </script>

                <script>

                    function exportOrderList() {
                        let startTime = $('#startTime').datebox('getValue')
                        let endTime = $('#endTime').datebox('getValue')
                        let status = $('#status').combobox('getValue')
                        let ordType = 'processing'
                        var form = [];
                        form.push('startTime=' + (startTime ? startTime : ''));
                        form.push('endTime=' + (endTime ? endTime : ''));
                        form.push('status=' + (status ? status : ''));
                        form.push('ordType=processing');
                        let url = 'api/purchase/order/export'
                        let xhr = new XMLHttpRequest()
                        xhr.open('POST', url, true)
                        xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
                        xhr.setRequestHeader('token', localStorage.getItem('token'));
                        xhr.setRequestHeader('userType', localStorage.getItem('userType'));
                        xhr.responseType = 'blob'
                        xhr.onload = function () {
                            if (this.status == 200) {
                                let blob = this.response
                                let reader = new FileReader()
                                reader.readAsDataURL(blob)
                                reader.onload = function (e) {
                                    let a = document.createElement('a')
                                    a.download = '采购订单' + new Date().toLocaleDateString() + '-' + new Date().toLocaleTimeString() + '.xlsx'
                                    a.href = e.target.result
                                    $('body').append(a)
                                    a.click()
                                    $(a).remove()
                                }
                            }
                        }
                        xhr.send(form.join('&'))
                    }
                    /**
                     *  按条件查询报价列表
                     */
                    function selectList() {
                        var startTime = $('#startTime').datebox('getValue')
                        var endTime = $('#endTime').datebox('getValue')
                        var offerStatus = $('#status').combobox('getValue');
                        var dg = $('#dg');
                        dg.datagrid('enableFilter');
                        dg.datagrid('load', {
                            startTime: startTime ? startTime : '',
                            endTime: endTime ? endTime : '',
                            status: offerStatus ? offerStatus : '',
                            ordType: 'processing'
                        });
                    }

                    /**
                     *打开供应商报价
                     */
                    function openOfferDetail(index) {
                        let dg = $('#dg')
                        const rows = dg.datagrid('getRows')
                        let row = rows[index]
                        setOfferDetailWindowValue(row)
                    }

                    /**
                     * 编辑报价信息
                     */
                    function editRow() {
                        let offerRow = $('#dg').datagrid('getSelected');
                        if (!offerRow) {
                            $.messager.alert('提示', '请选择一条数据');
                            return
                        }
                        if (+offerRow.status == 1) {
                            $.messager.alert('提示', '该报价已提交，不允许再次修改')
                            return
                        }

                        setOfferDetailWindowValue(offerRow, 'edit');
                    }

                    function initTabBtn() {
                        $('#tabs').tabs({
                            tools: [
                                {
                                    text: '保存',
                                    iconCls: 'icon-save',
                                    handler: function () {
                                        saveSupplyOfferMM('save');
                                    }
                                }, {
                                    text: '提交',
                                    iconCls: 'icon-ok',
                                    handler: function () {
                                        $.messager.confirm('提示', '提交后无法再次编辑，确定要提交吗?',
                                            function (r) {
                                                if (r) {
                                                    saveSupplyOfferMM('submit');
                                                }
                                            });
                                    }
                                }]
                        })
                    }

                    /**
                     * 报价详情(编辑)窗口赋值
                     */

                    function setOfferDetailWindowValue(item, action) {
                        console.log('item...', item)
                        if (+item.status != '1') {
                            initTabBtn();
                        } else {
                            $('#tabs').tabs({
                                tools: []
                            })
                        }

                        $.ajax({
                            type: 'get',
                            dataType: 'json',
                            url: 'purchase/order/selectInfor?id=' + item.ordId,
                            success: function (data) {
                                if (data.code != 60200) {
                                    $.messager.alert('消息', data.msg);
                                } else {
                                    var orderInforData = data.data;
                                    $('#infName').val(item.infName);
                                    $('#infCode').val(item.infCode);
                                    $('#infId').val(item.infId)
                                    $('#ordId').val(item.ordId)
                                    $('#ordCode').textbox('setValue', item.ordCode);
                                    $('#ordName').textbox('setValue', item.ordName);
                                    $('#inqContractStart').datebox('setValue', item.inqContractStart)
                                    $('#inqContractEnd').datebox('setValue', item.inqContractEnd)
                                    $('#resId').val(item.resId);
                                    // $('#txtCalCode').textbox('setValue', item.ordFax);
                                    // $('#txtDetail').textbox('setValue', item.ordDetail);
                                    if (orderInforData.supplyDeal.inqPaymentCode) {
                                        $('#cobPayment').combobox('setValue', orderInforData.supplyDeal.inqPaymentCode);
                                    } else {
                                        $('#cobPayment').combobox('setValue', 'Z001');
                                    }
                                    if (orderInforData.supplyDeal.inqCurrencyCode) {
                                        $('#inqCurrencyCode').combobox('setValue', orderInforData.supplyDeal.inqCurrencyCode);
                                    } else {
                                        $('#inqCurrencyCode').combobox('setValue', 'CNY');
                                    }
                                    if (orderInforData.supplyDeal.inqIsWeight) {
                                        $('#inqIsWeight').combobox('setValue', orderInforData.supplyDeal.inqIsWeight);
                                    }
                                    if (orderInforData.supplyDeal.inqIsSettle) {
                                        $('#inqIsSettle').combobox('setValue', orderInforData.supplyDeal.inqIsSettle);
                                    }
                                    if (orderInforData.supplyDeal.inqIsOwn) {
                                        $('#inqIsOwn').combobox('setValue', orderInforData.supplyDeal.inqIsOwn);
                                    }
                                    $('#ordContractType').combobox('setValue', orderInforData.ordContractType);
                                    $('#cobPayMode').combobox('setValue', orderInforData.ordPayModeCode);
                                    $('#ordFreight').textbox('setValue', orderInforData.ordFreight);
                                    // $('#cobCheck').combobox('setValue', orderInforData.ordCheckCode);
                                    // $('#inqEndTime').datebox('setValue', item.inqEndTime);
                                    $('#remark').val(item.remark);
                                    $('#createTime').datebox('setValue', item.createTime);
                                    // $('#infContact').val(item.infContact);
                                    // $('#infContactTel').val(item.infContactTel);
                                    // $('#gramDeptName').val(item.gramDeptName);
                                    // $('#gramGetDeptCode').val(item.gramGetDeptCode);
                                    // $('#gramIsUrgent').val(item.gramIsUrgent);
                                    // $('#gramFlowType').val(item.gramFlowType);
                                    // $('#createTime').val(item.createTime);
                                    $('#createName').val(item.createName);
                                    //    $('#updateName').val(item.updateName);
                                    //   $('#updateTime').datebox('setValue', item.updateTime);

                                    // 构建物资报价明细的dataGrid
                                    const purchaseSupplyOfferMms = orderInforData.supplyDealMms;
                                    setMmsDatagird(purchaseSupplyOfferMms, action);
                                    setSuppliesDatagird(orderInforData.supplyDeals, action);
                                    getFileList(item);
                                }
                            },
                            error: function (XMLHttpRequest, textStatus, errorThrown) {
                                $.messager.alert('消息', errorThrown);
                            }
                        })
                        $('#orderWindow').window('open');
                        $('#tabs').tabs('select', 0);
                    }


                    /**
                     * 报价明细编辑完成后的方法
                     */
                    function putDataToTempArr(PurchaseSupplyDealVoModel) {
                        // 将报价明细放入临时数组
                        const rows = $('#planMmDetailList').datagrid('getRows')
                        if (rows && rows.length > 0) {
                            //成交总数量
                            let totalDealNum = 0
                            // 成交单价
                            let totalDealUnitPrice = 0
                            // 成交总金额（中标总金额）
                            let dealTotalPriceT = 0
                            // 运费金额
                            let inqTransportTotalPrice = 0
                            for (let row of rows) {
                                /**
                                 *  将每个供应商的成交数量、成交单价、成交总金额、运费汇总
                                 */
                                let dealNum = row.dealNum ? Number(row.dealNum) : 0
                                totalDealNum += dealNum

                                let dealUnitPrice = row.dealUnitPrice ? Number(row.dealUnitPrice) : 0

                                // 成交总金额
                                row.dealTotalPrice = Number(dealNum) * Number(dealUnitPrice)
                                dealTotalPriceT += row.dealTotalPrice

                                // 运费单价
                                let inqTransportUnitPrice = row.inqTransportTaxPrice ?
                                    row.inqTransportTaxPrice / (1 + row.inqTransportTaxRate / 100) : 0;
                                inqTransportUnitPrice = inqTransportUnitPrice.toFixed(2);
                                row.inqTransportUnitPrice = inqTransportUnitPrice;
                                // 运费总价
                                row.inqTransportTotalPrice = Number(dealNum) * Number(inqTransportUnitPrice)
                                inqTransportTotalPrice += row.inqTransportTotalPrice
                            }
                            PurchaseSupplyDealVoModel.infLastTotalPrice = dealTotalPriceT;
                            PurchaseSupplyDealVoModel.infNum = totalDealNum;
                            if (totalDealNum != 0) {
                                totalDealUnitPrice = Number(dealTotalPriceT) / totalDealNum;
                            }
                            PurchaseSupplyDealVoModel.infUnitPrice = totalDealUnitPrice.toFixed(2);
                            PurchaseSupplyDealVoModel.infTransportCost = inqTransportTotalPrice;
                        }
                    }

                    // 保存/提交
                    function saveSupplyOfferMM(action) {
                        let offerStatus = '0';
                        if (action == 'submit') {
                            offerStatus = "1";
                        }
                        // 1.首先关闭编辑器
                        if (!endEditing()) {
                            return;
                        }
                        $('#planMmDetailList').datagrid('acceptChanges');
                        if (!$('#supplyOfferForm').form('validate')) {
                            $.messager.alert('消息', '有未填写数据！');
                            return;
                        }

                        // 2.获取物资明细
                        const planMmDetailList = $('#planMmDetailList').datagrid('getRows');
                        if (!planMmDetailList || planMmDetailList.length == 0) {
                            $.messager.alert('消息', '请选择物资');
                            return;
                        }
                        for (let item of planMmDetailList) {
                            if (!item.planFactoryCode) {
                                $.messager.alert('消息', item.mmCode + ':请选择到货工厂!');
                                return;
                            }
                            if (!item.dealNum) {
                                $.messager.alert('消息', item.mmCode + ':请录入数量!');
                                return;
                            }
                        }

                        let suppliesList = $('#suppliesList');
                        var supplys = suppliesList.datagrid('getRows');
                        if (!supplys || supplys.length == 0) {
                            $.messager.alert('消息', '请选择供应商');
                            return;
                        }
                        if (supplys.length > 1) {
                            $.messager.alert('消息', '只能选择1个供应商');
                            return;
                        }

                        // 调用后台接口保存
                        let PurchaseSupplyDealVoModel = {
                            resId: $('#resId').val(),
                            infId: supplys[0].infId,
                            infName: supplys[0].infName,
                            infCode: supplys[0].infCode,
                            inqId: '',
                            ordCode: $('#ordCode').textbox('getValue'),
                            ordName: $('#ordName').textbox('getValue'),
                            inqPaymentCode: $('#cobPayment').combobox('getValue'),
                            inqPayment: $('#cobPayment').combobox('getText'),
                            inqCurrencyCode: $('#inqCurrencyCode').combobox('getValue'),
                            inqCurrency: $('#inqCurrencyCode').combobox('getText'),
                            inqIsWeight: $('#inqIsWeight').combobox('getValue'),
                            inqIsSettle: $('#inqIsSettle').combobox('getValue'),
                            inqIsOwn: $('#inqIsOwn').combobox('getValue'),
                            status: offerStatus,
                            inqSapStatus: '0',
                            inqContractStart: $('#inqContractStart').datebox('getValue'),
                            inqContractEnd: $('#inqContractEnd').datebox('getValue'),
                            // gramId: $('#agreeGrid').combogrid('getValue'),
                            // infContact: $('#infContact').val(),
                            // infContactTel: $('#infContactTel').val(),
                            // supplyDealMms: planMmDetailList
                        };
                        putDataToTempArr(PurchaseSupplyDealVoModel);

                        var orderModel = {
                            resName: $('#resName').val(),
                            resId: $('#resId').val(),
                            ordId: $('#ordId').val(),
                            infId: supplys[0].infId,
                            infName: supplys[0].infName,
                            infCode: supplys[0].infCode,
                            ordCode: $('#ordCode').textbox('getValue'),
                            ordName: $('#ordName').textbox('getValue'),
                            ordType: 'processing',
                            ordCurrencyCode: $('#inqCurrencyCode').combobox('getValue'),
                            ordCurrency: $('#inqCurrencyCode').combobox('getText'),
                            // ordDetail: $('#txtDetail').textbox('getValue'),
                            // ordFax: $('#txtCalCode').textbox('getValue'),
                            // ordCheckCode: $('#cobCheck').combobox('getValue'),
                            // ordCheck: $('#cobCheck').combobox('getText'),
                            ordPayModeCode: $('#cobPayMode').combobox('getValue'),
                            ordPayMode: $('#cobPayMode').combobox('getText'),
                            ordFreight: $('#ordFreight').textbox('getValue'),
                            // ordIncidental: $('#ordIncidental').textbox('getValue'),
                            // ordSignAddress: $('#ordSignAddress').textbox('getValue'),
                            // ordSignDate: $('#ordSignDate').datebox('getValue'),
                            // ordResultDate: $('#ordResultDate').datebox('getValue'),
                            ordIsSign: '0',
                            // ordContractId: $('#ordContractId').val(),
                            // ordContractName: $('#ordContractName').textbox('getValue'),
                            inqContractStart: $('#inqContractStart').datebox('getValue'),
                            inqContractEnd: $('#inqContractEnd').datebox('getValue'),
                            ordContractType: $('#ordContractType').combobox('getValue'),
                            remark: $('#remark').textbox('getValue'),
                            supplyDeal: PurchaseSupplyDealVoModel,
                            supplyDealMms: planMmDetailList,
                            planMms: [],
                            status: '0'
                        }
                        $.ajax({
                            type: "post",
                            dataType: "json",
                            url: 'processing/order/save',
                            data: JSON.stringify(orderModel),
                            contentType: "application/json",
                            success: function (data) {
                                if (data.code != 60200) {
                                    $.messager.alert('消息', data.msg);
                                } else {
                                    $('#ordCode').textbox('setValue', data.data.ordCode);
                                    orderModel = data.data;
                                    saveAttch(orderModel.ordId, orderModel, action)
                                    if ('save' == action) {
                                        $.messager.alert('消息', data.msg);
                                        let dg = $('#dg');
                                        dg.datagrid('enableFilter');
                                        dg.datagrid('load');
                                        $('#orderWindow').window('close');
                                    }
                                }
                            },
                            error: function (XMLHttpRequest, textStatus, errorThrown) {
                                $.messager.alert('消息', errorThrown);
                            }
                        })
                    }

                    //提交
                    function submitData(orderModel) {
                        orderModel.status = 1;
                        $.ajax({
                            type: "post",
                            dataType: "json",
                            url: 'processing/order/commit',
                            data: JSON.stringify(orderModel),
                            contentType: "application/json",
                            success: function (data) {
                                $.messager.alert('消息', data.msg);
                                if (data.code != 60200) {
                                } else {
                                    let dg = $('#dg');
                                    dg.datagrid('enableFilter');
                                    dg.datagrid('load');
                                    $('#orderWindow').window('close');
                                }
                            },
                            error: function (XMLHttpRequest, textStatus, errorThrown) {
                                $.messager.alert('消息', errorThrown);
                            }
                        })
                    }



                    /**
                     * 查看报价详情
                     */
                    function viewDetails() {
                        let offerRow = $('#dataGridDiv').datagrid('getSelected');
                        if (!offerRow) {
                            $.messager.alert('提示', '请选择一条数据');
                            return
                        }
                        if (+offerRow.status != '1') {
                            $('#tabs').tabs({
                                tools: [{
                                    text: '提交',
                                    iconCls: 'icon-ok',
                                    handler: function () {
                                        $.messager.confirm('提示', '提交后无法再次编辑，确定要提交吗?',
                                            function (r) {
                                                if (r) {
                                                    saveSupplyOfferMM('submit');
                                                }
                                            });
                                    }
                                }]
                            })
                        } else {
                            $('#tabs').tabs({
                                tools: []
                            })
                        }

                        setOfferDetailWindowValue(offerRow)
                    }

                    function bindGrid() {
                        let dg = $('#dg');
                        let startTime = $('#startTime').datebox('getValue') ? $('#startTime').datebox('getValue') : ''
                        let endTime = $('#endTime').datebox('getValue') ? $('#endTime').datebox('getValue') : ''
                        let status = $('#status').combobox('getValue') ? $('#status').combobox('getValue') : ''
                        dg.datagrid({
                            title: '',
                            queryParams: {
                                startTime: startTime,
                                endTime: endTime,
                                status: status,
                                ordType: 'processing'
                            },
                            fitColumns: false,
                            fit: false,
                            url: 'purchase/order/selectList',
                            iconCls: 'icon-application',
                            clientPaging: false,
                            remoteFilter: true,
                            filterDelay: 0,
                            width: getWidth(0.98),
                            height: 400,
                            nowrap: true,
                            rownumbers: true,
                            animate: false,
                            collapsible: false,
                            singleSelect: true,
                            pageSize: 10,
                            remoteSort: false,
                            pagination: true,
                            frozenColumns: [[
                                {
                                    field: 'infId',
                                    title: '供应商id',
                                    hidden: true,
                                    formatter: function (val, rec, index) {
                                        return val ? val : '';
                                    }
                                },
                                {
                                    field: 'resId',
                                    title: '询价id',
                                    hidden: true,
                                    formatter: function (val, rec, index) {
                                        return val ? val : '';
                                    }
                                },
                                {
                                    field: 'ordCode',
                                    title: '订单编码',
                                    formatter: function (val, rec, index) {
                                        return val ? val : '';
                                    }
                                },
                                {
                                    field: 'ordName',
                                    title: '订单名称',
                                    sortable: true,
                                    formatter: function (val, rec, index) {
                                        return '<a href="javascript:void(0)" onclick="openOfferDetail(' + index +
                                            ')">' + (val ? val : '') + '</a>'
                                        // return '<a href="javascript:void(0)" onclick="setOfferDetailWindowValue(' + index+')">' + (val ? val : '') + '</a>'
                                    }
                                }
                            ]],
                            columns: [[
                                {
                                    field: 'infName',
                                    title: '供应商名称',
                                    sortable: true,
                                    formatter: function (val, rec, index) {
                                        return unescape(val ? val : '')

                                    }
                                },
                                {
                                    field: 'infCode',
                                    title: '供应商编码',
                                    sortable: true,
                                    formatter: function (val, rec, index) {
                                        return unescape(val ? val : '')

                                    }
                                },
                                {
                                    field: 'ordFreight',
                                    title: '费用',
                                    sortable: true,
                                    formatter: function (val, rec) {
                                        return unescape(val ? val : '')
                                    }
                                },
                                {
                                    field: 'inqContractStart',
                                    title: '合同起始日期',
                                    sortable: true,
                                    formatter: function (val, rec) {
                                        return unescape(val ? val : '')
                                    }
                                },
                                {
                                    field: 'inqContractEnd',
                                    title: '合同结束日期',
                                    sortable: true,
                                    formatter: function (val, rec) {
                                        return unescape(val ? val : '')
                                    }
                                },
                                {
                                    field: 'ordPayMode',
                                    title: '付款方式',
                                    sortable: true,
                                    formatter: function (val, rec) {
                                        return unescape(val ? val : '')
                                    }
                                },
                                {
                                    field: 'remark',
                                    title: '备注',
                                    sortable: true,
                                    formatter: function (val, rec) {
                                        return unescape(val ? val : '')
                                    }
                                },
                                {
                                    field: 'ordContractType',
                                    title: '是否推送OA',
                                    sortable: true,
                                    formatter: function (val, rec) {
                                        return unescape(val == '1' ? '是' : '否')
                                    }
                                },
                                {
                                    field: 'createTime',
                                    title: '创建时间',
                                    sortable: true,
                                    formatter: function (val, rec) {
                                        return unescape(val ? val : '')
                                    }
                                },
                                {
                                    field: 'oaFlowId',
                                    title: 'oa流程id',
                                    sortable: true,
                                    formatter: function (val, rec) {
                                        return unescape(val ? val : '')
                                    }
                                },
                                {
                                    field: 'createName',
                                    title: '创建人',
                                    sortable: true,
                                    formatter: function (val, rec) {
                                        return unescape(val ? val : '')
                                    }
                                },
                                {
                                    field: 'updateTime',
                                    title: '更新时间',
                                    sortable: true,
                                    formatter: function (val, rec) {
                                        return unescape(val ? val : '')
                                    }
                                },
                                {
                                    field: 'status',
                                    title: '状态',
                                    sortable: true,
                                    formatter: function (val, rec) {
                                        let status = val ? val : '';
                                        return unescape(status == '0' ? '待提交' : (status == '2' ? '已完成' : '已提交'));
                                    }
                                }
                            ]],
                            toolbar: [
                                {
                                    text: '新增',
                                    iconCls: 'icon-add',
                                    handler: function () {
                                        $('#supplyOfferForm').form('clear');
                                        setMmsDatagird([], 'edit');
                                        initTabBtn();
                                        // 清空供应商明细
                                        setSuppliesDatagird([], 'add');
                                        $('#orderWindow').window('open');
                                        $('#tabs').tabs('select', 0);

                                    }
                                },
                                {
                                    text: '编辑',
                                    iconCls: 'icon-edit',
                                    handler: function () {
                                        $('#supplyOfferForm').form('clear');
                                        editRow();
                                    }
                                }
                                , '-',
                                {
                                    text: '删除',
                                    iconCls: 'icon-cancel',
                                    handler: function () {
                                        //删除
                                        let row = $('#dg').datagrid('getSelected');
                                        if (row) {
                                            if (+row.status == '1') {
                                                $.messager.confirm('提示', '该数据已提交，无法删除')
                                                return
                                            } else {
                                                $.messager.confirm('提示', '确定要删除吗?',
                                                    function (r) {
                                                        if (r) {
                                                            deleteInquiryResult(row.ordId, row.resId);
                                                        }
                                                    });
                                            }

                                        } else {
                                            $.messager.alert('消息', '请选中一个要删除的数据');
                                        }
                                    }
                                }
                                , '-',
                                {
                                    text: '撤回',
                                    iconCls: 'icon-undo',
                                    handler: function () {
                                        //撤回
                                        let row = $('#dg').datagrid('getSelected');
                                        if (row) {
                                            //         if (row.status == '2') {
                                            // $.messager.alert('提示', '该单据已完成，无法撤回')
                                            //             return
                                            //         }
                                            if (row.status != '1') {
                                                $.messager.alert('提示', '该单据未提交，无法撤回')
                                                return
                                            }
                                            $.messager.confirm('提示', '确定要撤回该订单吗?',
                                                function (r) {
                                                    if (r) {
                                                        backupOrder(row);
                                                    }
                                                });
                                        } else {
                                            $.messager.alert('消息', '请选择要撤回的订单。');
                                            return
                                        }
                                    }
                                }
                            ],
                            onLoadSuccess: function () {

                            },
                            onDblClickRow: function (rowIndex, rowData) {
                                editRow();
                            }
                        });
                        dg.datagrid('enableFilter');
                    }


                    /**
                     *  撤回采购订单
                     */
                    function backupOrder(orderObj) {
                        $.ajax({
                            type: 'post',
                            url: 'purchase/order/back/?ordId=' + orderObj.ordId,
                            data: JSON.stringify(orderObj),
                            contentType: "application/json",
                            dataType: 'json',
                            success: function (data) {
                                if (data.code == 60200) {
                                    $.messager.alert('消息', data.msg)
                                    let dg = $('#dg');
                                    dg.datagrid('enableFilter');
                                    dg.datagrid('load');
                                } else {
                                    $.messager.alert('消息', data.msg)
                                }
                            },
                            error: function (XMLHttpRequest, textStatus, errorThrown) {
                                $.messager.alert('消息', errorThrown);
                            }
                        })
                    }

                    /**
                     *删除询价结果
                     **/
                    function deleteInquiryResult(resId, ordId) {
                        $.ajax({
                            type: "delete",
                            url: "purchase/order/deleteSpecial?ordId=" + ordId + "&resId=" + resId,
                            // data: JSON.stringify(queryData),
                            success: function (data) {
                                $.messager.alert('消息', data.msg);
                                if (data.code != 60200) {
                                } else {
                                    let dg = $('#dg');
                                    dg.datagrid('enableFilter');
                                    dg.datagrid('load');
                                }
                            },
                            error: function (XMLHttpRequest, textStatus, errorThrown) {
                                $.messager.alert('消息', errorThrown);
                            }
                        });
                    }

                </script>
                <script>
                    /**
                     * 删除物资
                     * @param data
                     */
                    function delProgramMm(row, index) {
                        console.log('row:', row)
                        if (!row.resId) {
                            console.log('delProgramMm', index, row)
                            $('#planMmDetailList').datagrid('deleteRow', index)
                            return
                        }
                        $.ajax({
                            type: 'delete',
                            dataType: 'json',
                            url: 'purchase/result/delResultMm?resId=' + row.resId + '&infId=' + row.infId + '&planId=' + row.planId,
                            success: function (data) {
                                $.messager.alert('消息', data.msg);
                                if (data.code == 60200) {
                                    $('#planMmDetailList').datagrid('deleteRow', index)
                                }
                            },
                            error: function (XMLHttpRequest, textStatus, errorThrown) {
                                $.messager.alert('消息', errorThrown);
                            }
                        })
                    }

                    // 移除所选
                    function removeMm(el) {
                        if (el == 'mm') {
                            let row = $('#planMmDetailList').datagrid('getSelected');
                            if (!row) {
                                $.messager.alert('提示', '请选择一条数据');
                                return
                            }
                            // 获取索引
                            let index = $('#planMmDetailList').datagrid('getRowIndex', row);
                            delProgramMm(row, index)
                        } else if (el == 'supply') {
                            let row = $('#suppliesList').datagrid('getSelected');
                            if (!row) {
                                $.messager.alert('提示', '请选择一条数据');
                                return
                            }
                            // 获取索引
                            let index = $('#suppliesList').datagrid('getRowIndex', row);
                            $('#suppliesList').datagrid('deleteRow', index)
                        }
                    }

                    // 构建物资明细dataGrid
                    function setMmsDatagird(data, action) {
                        var mmToolBar = [];
                        if (action == 'detail') {
                            mmToolBar = [];
                        } else {
                            // 物资列表的按钮工具栏
                            mmToolBar = [{
                                text: '新增',
                                iconCls: 'icon-add',
                                handler: function () {
                                    // 打开计划物资的弹窗
                                    selectMMWindow();
                                }
                                // }, '-', {
                                //     text: '编辑',
                                //     iconCls: 'icon-edit',
                                //     handler: function () {
                                //         editPlanRow();
                                //     }
                            }, '-', {
                                text: '移除',
                                iconCls: 'icon-remove',
                                handler: function () {
                                    let row = $('#dg').datagrid('getSelected');
                                    if (row) {
                                        if (+row.status == '1') {
                                            $.messager.confirm('提示', '该数据已提交，无法删除')
                                            return
                                        }
                                    }
                                    $.messager.confirm("提示", "确认移除?", function (e) {
                                        if (e) {
                                            removeMm('mm');
                                        }
                                    })
                                }
                            },];
                        }
                        let planMmDetailList = $('#planMmDetailList')
                        planMmDetailList.datagrid({
                            //             fitColumns: true,
                            data: data,
                            iconCls: 'icon-application',
                            clientPaging: false,
                            filterDelay: 0,
                            width: 850,
                            height: 300,
                            nowrap: true,
                            rownumbers: true,
                            animate: false,
                            collapsible: false,
                            singleSelect: true,
                            frozenColumns: [
                                [
                                    // {
                                    //     field: 'planCode',
                                    //     title: '计划编码',
                                    //     sortable: true,
                                    //     width: 100,
                                    //     formatter: function (val, rec) {
                                    //         return unescape(val ? val : '')
                                    //     }
                                    // },
                                    {
                                        field: 'mmCode',
                                        title: '物资编码',
                                        width: 100,
                                        sortable: true,
                                        formatter: function (val, rec) {
                                            return unescape(val ? val : '')
                                        }
                                    },
                                    {
                                        field: 'mmDetail',
                                        title: '物资描述',
                                        sortable: true,
                                        width: 210,
                                        formatter: function (val, rec) {
                                            if (val) {
                                                return '<span title="' + val + '">' + val + '</span>';
                                            } else {
                                                return val;
                                            }
                                        }
                                    }
                                ]
                            ],
                            columns: [[
                                {
                                    field: 'planFactoryCode',
                                    title: '到货工厂', editor: {
                                        type: 'combobox',
                                        options: {
                                            url: 'sysDict/queryItemByTypeName?dicName=交货工厂',
                                            method: 'get',
                                            valueField: "dicValue",
                                            textField: "dicName",
                                            editable: false,
                                            required: true,
                                            onSelect: function (record) {
                                                let proRow = $('#planMmDetailList').datagrid('getSelected')
                                                let rowIndex = $("#planMmDetailList").datagrid('getRowIndex', proRow);
                                                let arrivalFactoryTarget = $('#planMmDetailList').datagrid('getEditor', {
                                                    index: rowIndex,
                                                    field: 'planFactoryCode'
                                                }).target;
                                                let value = arrivalFactoryTarget.combobox('getValue');
                                                KCDD_URL = getRepertoryAddress(value)
                                                //此处field为第二个下拉列的field
                                                let appTarget = $('#planMmDetailList').datagrid('getEditor', {
                                                    index: rowIndex,
                                                    field: 'inqStockAddressCode'
                                                }).target;
                                                //清空
                                                appTarget.combobox('clear');
                                                appTarget.combobox('reload', KCDD_URL);
                                            }
                                        }
                                    },
                                    width: 200,
                                    formatter: function (val, rec) {
                                        return val ? val : ''
                                    },
                                },
                                {
                                    field: 'inqStockAddressCode',
                                    title: '库存地点',
                                    width: 200,
                                    editor: {
                                        type: 'combobox',
                                    },
                                    formatter: function (val, rec) {
                                        return rec.inqStockAddress;
                                    }, styler: cellStyler
                                },
                                {
                                    field: 'dealNum',
                                    title: '数量',
                                    width: 100,
                                    sortable: true,
                                    editor: {
                                        type: 'numberbox',
                                        options: { required: true, precision: 4 }
                                    },
                                    formatter: function (val, rec) {
                                        return unescape(val ? val : '')
                                    }
                                },
                                {
                                    field: 'mmUnit',
                                    title: '计量单位',
                                    sortable: true,
                                    width: 100,
                                    formatter: function (val, rec) {
                                        return unescape(val ? val : '')
                                    }
                                },
                                {
                                    field: 'mmUnitDetail',
                                    title: '计量单位描述',
                                    sortable: true,
                                    formatter: function (val, rec) {
                                        return unescape(val ? val : '')
                                    }
                                },
                                // {
                                //     field: 'dealUnitPrice',
                                //     title: '单价',
                                //     width: 100,
                                //     sortable: true,
                                //     editor: {
                                //         type: 'numberbox',
                                //         options: { required: true, precision: 2 }
                                //     },
                                //     formatter: function (val, rec) {
                                //         return unescape(val ? val : '')
                                //     }
                                // },
                                // {
                                //     field: 'dealTotalPrice',
                                //     title: '金额',
                                //     width: 100,
                                //     sortable: true,
                                //     formatter: function (val, rec) {
                                //         return unescape(val ? val : '')
                                //     }
                                // },
                                // {
                                //     field: 'inqTaxNum',
                                //     title: '税码',
                                //     width: 100,
                                //     editor: {
                                //         type: 'combobox',
                                //         options: {
                                //             url: 'sysDict/queryItemByTypeName?dicName=税码',
                                //             method: 'get',
                                //             valueField: "dicValue",
                                //             textField: "dicName",
                                //             editable: false,
                                //             required: false
                                //         }
                                //     },
                                //     formatter: function (val, rec) {
                                //         return rec.inqTaxRate;
                                //     }, styler: cellStyler
                                // },
                                // {
                                //     field: 'inqTransportTaxPrice',
                                //     title: '运费单价',
                                //     editor: {
                                //         type: 'numberbox',
                                //         options: { required: true, precision: 4 }
                                //     },
                                //     formatter: function (val, rec) {
                                //         return val ? unescape(val) : '0'
                                //     }, styler: cellStyler
                                // },
                                // {
                                //     field: 'inqTransportTaxRate',
                                //     title: '运费税码',
                                //     width: 70,
                                //     editor: {
                                //         type: 'combobox',
                                //         options: {
                                //             url: 'sysDict/queryItemByTypeName?dicName=运费税率',
                                //             method: 'get',
                                //             valueField: "dicValue",
                                //             textField: "dicName",
                                //             editable: false,
                                //             required: true
                                //         }
                                //     },
                                //     formatter: function (val, rec) {
                                //         return rec.inqTransportTaxRate;
                                //     }, styler: cellStyler
                                // },
                                // {
                                //     field: 'inqBackGood',
                                //     title: '退货标记',
                                //     formatter: function (val, rec) {
                                //         return val == 'X' ? '退货' : '采购'
                                //     },
                                //     editor: {
                                //         type: 'combobox',
                                //         options: {
                                //             data: inqBackGoodOptions,
                                //             valueField: "inqBackGood",
                                //             textField: "text",
                                //             editable: false,
                                //             required: true
                                //         }
                                //     }, styler: cellStyler
                                // },
                                // {
                                //     field: 'inqIsFree',
                                //     title: '免费标记',
                                //     formatter: function (val, rec) {
                                //         return val == 'X' ? '免费' : '付费'
                                //     },
                                //     editor: {
                                //         type: 'combobox',
                                //         options: {
                                //             data: inqIsFreeOptions,
                                //             valueField: "inqIsFree",
                                //             textField: "text",
                                //             editable: false,
                                //             required: true
                                //         }
                                //     }, styler: cellStyler
                                // },
                                {
                                    field: 'planNedType',
                                    title: '需求类型',
                                    sortable: true,
                                    formatter: function (val, rec) {
                                        return unescape(val ? val : '')
                                    }
                                },
                                {
                                    field: 'planGoodType',
                                    title: '资产/费用/存货',
                                    sortable: true,
                                    formatter: function (val, rec) {
                                        return unescape(val ? val : '')
                                    }
                                },
                                {
                                    field: 'planDeptCode',
                                    title: '使用单位编码',
                                    sortable: true,
                                    formatter: function (val, rec) {
                                        return unescape(val ? val : '')
                                    }
                                },
                                {
                                    field: 'planCorpCode',
                                    title: '公司编码',
                                    sortable: true,
                                    formatter: function (val, rec) {
                                        return unescape(val ? val : '')
                                    }
                                },
                                // {
                                //     field: 'planIsContract',
                                //     title: '是否合同',
                                //     sortable: true,
                                //     formatter: function (val, rec) {
                                //         return unescape(val ? val : '')
                                //     }
                                // },
                                {
                                    field: 'createTime',
                                    title: '创建时间',
                                    sortable: true,
                                    formatter: function (val, rec) {
                                        return unescape(val ? val : '')
                                    }
                                },
                                {
                                    field: 'updateTime',
                                    title: '更新时间',
                                    sortable: true,
                                    formatter: function (val, rec) {
                                        return unescape(val ? val : '')
                                    }
                                },
                                {
                                    field: 'status',
                                    title: '状态',
                                    sortable: true,
                                    formatter: function (val, rec) {
                                        let status = val ? val : '';
                                        return unescape(status == '0' ? '待提交' : (status == '2' ? '已完成' : '已提交'));
                                    }
                                }
                            ]
                            ],
                            onEndEdit: function (index, row) {
                                var ed = $(this).datagrid('getEditor', {
                                    index: index,
                                    field: 'inqStockAddressCode'
                                });
                                row.inqStockAddress = $(ed.target).combobox('getText');

                                // ed = $(this).datagrid('getEditor', {
                                //     index: index,
                                //     field: 'inqTaxNum'
                                // });
                                // row.inqTaxRate = $(ed.target).combobox('getText');

                                // ed = $(this).datagrid('getEditor', {
                                //     index: index,
                                //     field: 'inqTransportTaxRate'
                                // });
                                // row.inqTransportTaxRate = $(ed.target).combobox('getText');
                            },
                            onClickCell: onClickCellMm,
                            onLoadSuccess: function () {

                            },
                            toolbar: mmToolBar,
                        });
                        planMmDetailList.datagrid('enableFilter');
                    }

                    function onClickCellMm(index, field) {
                        if (editIndex != index) {
                            if (endEditing()) {
                                $('#planMmDetailList').datagrid('selectRow', index)
                                    .datagrid('beginEdit', index);
                                var ed = $('#planMmDetailList').datagrid('getEditor', { index: index, field: field });
                                var row = $('#planMmDetailList').datagrid('getRows')[index];

                                var addressUrl = 'sysDict/queryItemByTypeName?dicName=' + row.planFactoryCode + '-库存地点';
                                //此处field为第二个下拉列的field
                                let appTarget = $('#planMmDetailList').datagrid('getEditor', {
                                    index: index,
                                    field: 'inqStockAddressCode'
                                }).target;

                                if (appTarget.combobox('getData').length == 0) {
                                    //清空
                                    appTarget.combobox('clear');
                                    appTarget.combobox({
                                        url: addressUrl,
                                        method: 'get',
                                        valueField: "dicValue",
                                        textField: "dicName",
                                        editable: false,
                                        required: true
                                    });
                                    appTarget.combobox('setValue', row.inqStockAddressCode);
                                }
                                if (ed) {
                                    //  ($(ed.target).data('textbox') ? $(ed.target).textbox('textbox') : $(ed.target)).focus();
                                }
                                editIndex = index;
                            } else {
                                setTimeout(function () {
                                    $('#planMmDetailList').datagrid('selectRow', editIndex);
                                }, 0);
                            }
                        }
                    }

                    /**
                     * 打开计划物资的弹窗
                     */
                    function selectMMWindow() {
                        $('#mmDataGrid').datagrid('clearChecked');
                        selectMMListByParams();
                        $('#selectMMWindow').window('open');
                    }

                    /**
                     * 条件查询计划物资明细的方法
                     */
                    function selectMMListByParams() {
                        let startTime = $('#startMMTime').datebox('getValue')
                        let endTime = $('#endMMTime').datebox('getValue')
                        let mmDataGrid = $('#mmDataGrid');
                        mmDataGrid.datagrid('enableFilter');
                        mmDataGrid.datagrid('load', {
                            startTime: startTime ? startTime : '',
                            endTime: endTime ? endTime : '',
                            planNedType: 'ZB10',
                            status: '1'
                        });
                    }


                    function selectMMGrid() {
                        $('#startMMTime').datebox('setValue', getBeforeCurrentDate30Days());
                        $('#endMMTime').datebox('setValue', getNowFormatDate());
                        let mmDataGrid = $('#mmDataGrid');
                        mmDataGrid.datagrid({
                            queryParams: {
                                startTime: $('#startTime').datebox('getValue') ? $('#startTime').datebox('getValue') : '',
                                endTime: $('#endTime').datebox('getValue') ? $('#endTime').datebox('getValue') : '',
                                planNedType: 'ZB10',
                                status: '1'
                            },
                            fitColumns: true,
                            url: 'purchase/plan/selectPlanMmList?',
                            iconCls: 'icon-application',
                            clientPaging: false,
                            checkOnSelect: true,
                            remoteFilter: true,
                            filterDelay: 0,
                            width: 860,
                            height: 300,
                            nowrap: true,
                            rownumbers: true,
                            animate: false,
                            collapsible: false,
                            singleSelect: false,
                            pageSize: 20,
                            pageList: [20, 30, 40, 50],
                            remoteSort: true,
                            pagination: true,
                            frozenColumns: [
                                [{
                                    field: 'ck',
                                    checkbox: true,
                                    width: 100,
                                },
                                {
                                    field: 'mmId',
                                    title: '物资ID',
                                    hidden: true,
                                    width: 100,
                                    formatter: function (val, rec) {
                                        return unescape(val ? val : '')
                                    }
                                },
                                {
                                    field: 'planId',
                                    title: '采购计划ID',
                                    hidden: true,
                                    sortable: true,
                                    width: 100,
                                    formatter: function (val, rec) {
                                        return unescape(val ? val : '')
                                    }
                                },
                                {
                                    field: 'planCode',
                                    title: '计划编码',
                                    sortable: true,
                                    formatter: function (val, rec, index) {
                                        return (val ? val : '')
                                    }
                                },
                                {
                                    field: 'mmCode',
                                    title: '物资编码',
                                    width: 100,
                                    sortable: true,
                                    formatter: function (val, rec) {
                                        return unescape(val ? val : '')
                                    }
                                },
                                {
                                    field: 'mmDetail',
                                    title: '物资描述',
                                    sortable: true,
                                    width: 200,
                                    formatter: function (val, rec) {
                                        if (val) {
                                            return '<span title="' + val + '">' + val + '</span>';
                                        } else {
                                            return val;
                                        }
                                    }
                                },
                                ]
                            ],
                            columns: [
                                [
                                    {
                                        field: 'planPurNum',
                                        title: '采购计划量',
                                        sortable: true,
                                        formatter: function (val, rec) {
                                            return unescape(val ? val : '')
                                        }
                                    },
                                    {
                                        field: 'planDeptCode',
                                        title: '使用单位编码',
                                        sortable: true,
                                        formatter: function (val, rec) {
                                            return unescape(val ? val : '')
                                        }
                                    },
                                    {
                                        field: 'planDeptName',
                                        title: '使用单位名称',
                                        sortable: true,
                                        formatter: function (val, rec) {
                                            return unescape(val ? val : '')
                                        }
                                    },
                                    {
                                        field: 'planCorpCode',
                                        title: '公司编码',
                                        sortable: true,
                                        formatter: function (val, rec) {
                                            return unescape(val ? val : '')
                                        }
                                    },
                                    {
                                        field: 'planCorpName',
                                        title: '公司名称',
                                        sortable: true,
                                        formatter: function (val, rec) {
                                            return unescape(val ? val : '')
                                        }
                                    },
                                    {
                                        field: 'planIsContract',
                                        title: '是否合同',
                                        sortable: true,
                                        formatter: function (val, rec) {
                                            return unescape(val ? val : '')
                                        }
                                    },
                                    {
                                        field: 'planProType',
                                        title: '项目类型',
                                        sortable: true,
                                        formatter: function (val, rec) {
                                            return unescape(val ? val : '')
                                        }
                                    },
                                    {
                                        field: 'planGoodType',
                                        title: '资产/费用/存货',
                                        sortable: true,
                                        formatter: function (val, rec) {
                                            return unescape(val ? val : '')
                                        }
                                    },
                                    {
                                        field: 'mmTypeCode',
                                        title: '物资类别编码',
                                        sortable: true,
                                        formatter: function (val, rec) {
                                            return unescape(val ? val : '')
                                        }
                                    },
                                    {
                                        field: 'planAssessType',
                                        title: '评估类型',
                                        sortable: true,
                                        formatter: function (val, rec) {
                                            return unescape(val ? val : '')
                                        }
                                    },
                                    {
                                        field: 'planNeedDate',
                                        title: '需用日期',
                                        sortable: true,
                                        formatter: function (val, rec) {
                                            return unescape(val ? val : '')
                                        }
                                    },
                                    {
                                        field: 'mmUnit',
                                        title: '计量单位',
                                        sortable: true,
                                        formatter: function (val, rec) {
                                            return unescape(val ? val : '')
                                        }
                                    },
                                    {
                                        field: 'mmUnitDetail',
                                        title: '计量单位描述',
                                        sortable: true,
                                        formatter: function (val, rec) {
                                            return unescape(val ? val : '')
                                        }
                                    },
                                    {
                                        field: 'planMeanCode',
                                        title: '资产编码',
                                        sortable: true,
                                        formatter: function (val, rec) {
                                            return unescape(val ? val : '')
                                        }
                                    },
                                    {
                                        field: 'planMeanDetail',
                                        title: '资产描述',
                                        sortable: true,
                                        formatter: function (val, rec) {
                                            return unescape(val ? val : '')
                                        }
                                    },
                                    {
                                        field: 'planCostCode',
                                        title: '成本中心编码',
                                        sortable: true,
                                        formatter: function (val, rec) {
                                            return unescape(val ? val : '')
                                        }
                                    },
                                    {
                                        field: 'planCostDetail',
                                        title: '成本中心描述',
                                        sortable: true,
                                        formatter: function (val, rec) {
                                            return unescape(val ? val : '')
                                        }
                                    },
                                    {
                                        field: 'planOrderCode',
                                        title: '订单编码',
                                        sortable: true,
                                        formatter: function (val, rec) {
                                            return unescape(val ? val : '')
                                        }
                                    },
                                    {
                                        field: 'planOrderDetail',
                                        title: '订单描述',
                                        sortable: true,
                                        formatter: function (val, rec) {
                                            return unescape(val ? val : '')
                                        }
                                    },
                                    {
                                        field: 'planFactoryCode',
                                        title: '到货工厂',
                                        sortable: true,
                                        formatter: function (val, rec) {
                                            return unescape(val ? val : '')
                                        }
                                    },
                                    {
                                        field: 'planFactoryName',
                                        title: '到货工厂描述',
                                        sortable: true,
                                        formatter: function (val, rec) {
                                            return unescape(val ? val : '')
                                        }
                                    },
                                    {
                                        field: 'planUnderDate',
                                        title: '归口日期',
                                        sortable: true,
                                        formatter: function (val, rec) {
                                            return unescape(val ? val : '')
                                        }
                                    },
                                    {
                                        field: 'status',
                                        title: '状态',
                                        sortable: true,
                                        formatter: function (val, rec) {
                                            let status = val ? val : '';
                                            return unescape(status == '0' ? '待提交' : '已提交');
                                        }
                                    }
                                ]
                            ],
                            toolbar: [
                                //     {
                                //     text: '加入',
                                //     iconCls: 'icon-ok',
                                //     handler: function () {
                                //         addRowsToPlanMmDetailList();
                                //     }
                                // }, '-',
                                /* {
                                     text: '查看采购计划列表',
                                     iconCls: 'icon-search',
                                     handler: function () {
                                         setPlanGrid();
                                     }
                                 }*/
                            ],
                            onEndEdit: function (index, row) { },
                            onLoadSuccess: function () {
                            },
                            // 行单击事件
                            onClickRow: function (index, rowData) {
                            }
                        })
                        mmDataGrid.datagrid('enableFilter');
                    }

                    /**
                     * 将选中的计划物资添加到物资列表
                     */
                    function addRowsToPlanMmDetailList() {
                        let mmDataGrid = $('#mmDataGrid');
                        const checkedmmRows = mmDataGrid.datagrid('getChecked');
                        if (!checkedmmRows || checkedmmRows.length < 1) {
                            $.messager.alert('提示', '请至少选择一条数据');
                            return
                        }
                        if (checkedmmRows.length > 0 && checkedmmRows != null) {
                            // 增加判断：需求类型、采购组织、公司代码保持一致
                            let flag = checkPropertyOfSelectedMm(checkedmmRows)
                            if (!flag) {
                                $.messager.alert('提示', '请确定所选物资的需求类型、采购组织、公司代码、资产\\物料\\费用、是否合同、到货工厂保持一致')
                                return
                            }
                            // 与已添加进物资明细的数据做对比，planId相同的不再重复添加
                            const planMmrows = $('#planMmDetailList').datagrid('getRows')
                            console.log('planMmrows:', planMmrows);
                            for (let i in checkedmmRows) {
                                // planMM 转换programMM
                                let programMM = convertToPrograMM(checkedmmRows[i])
                                console.log('pro:', programMM);
                                if (planMmrows.length > 0 && planMmrows != null) {
                                    const result = planMmrows.some((item, index, arr) => {
                                        return item.mmCode == programMM.mmCode
                                    })
                                    if (!result) {
                                        $('#planMmDetailList').datagrid('insertRow', {
                                            row: programMM
                                        })
                                    }
                                } else {
                                    $('#planMmDetailList').datagrid('insertRow', {
                                        row: programMM
                                    })
                                }
                            }
                        }
                        $('#selectMMWindow').window('close');
                    }


                    // planMM 转换programMM
                    function convertToPrograMM(planMM) {
                        let programMM = {
                            mmId: planMM.mmId,
                            planId: planMM.planId,
                            mmCode: planMM.mmCode,
                            mmDetail: planMM.mmDetail,
                            mmCategory: planMM.mmCategory,
                            gramNeedNum: planMM.planNeedNum,
                            planPurNum: planMM.planPurNum,
                            gramPurNum: planMM.planPurNum,
                            mmMain: planMM.mmMain,
                            mmSecond: planMM.mmSecond,
                            mmNature: planMM.mmNature,
                            mmTypeCode: planMM.mmTypeCode,
                            mmTypeName: planMM.mmTypeName,
                            mmUnit: planMM.mmUnit,
                            mmUnitDetail: planMM.mmUnitDetail,
                            gramStockNum: planMM.planStockNum,
                            gramNeedDate: planMM.planNeedDate,
                            gramDevice: planMM.planDevice,
                            gramQm: planMM.planQm,
                            gramUse: planMM.planUse,
                            gramUseName: planMM.planUseName,
                            gramUseTel: planMM.planUseTel,
                            gramOutType: planMM.planOutType,
                            planTransfer: planMM.planTransfer,
                            gramRemark: planMM.planRemark,
                            planNedType: planMM.planNedType,
                            planCorpCode: planMM.planCorpCode,
                            planCorpName: planMM.planCorpName,
                            planDeptCode: planMM.planDeptCode,
                            planDeptName: planMM.planDeptName,
                            planIsContract: planMM.planIsContract,
                            planProType: planMM.planProType,
                            planGoodType: planMM.planGoodType,
                            planAssessType: planMM.planAssessType,
                            planMeanCode: planMM.planMeanCode,
                            planMeanDetail: planMM.planMeanDetail,
                            planCostCode: planMM.planCostCode,
                            planCostDetail: planMM.planCostDetail,
                            planOrderCode: planMM.planOrderCode,
                            planOrderDetail: planMM.planOrderDetail,
                            planFactoryCode: planMM.planFactoryCode,
                            planFactoryName: planMM.planFactoryName,
                            planIsReturn: planMM.planIsReturn,
                            planUnderDate: planMM.planUnderDate,
                            createTime: planMM.createTime,
                            createName: planMM.createName
                        }
                        return programMM;
                    }

                    function checkPropertyOfSelectedMm(selectedMMArr) {
                        let flag = true
                        // 需求类型
                        let planNedType = ''
                        // 使用公司
                        let planCorpCode = ''
                        // 采购组织
                        let planDeptCode = ''
                        //资产/费用/存货
                        let planGoodType = ''
                        // 是否合同
                        let isContract = ''
                        // 到货工厂
                        let planFactoryCodeTem = ''
                        const rows = $('#planMmDetailList').datagrid('getRows')
                        // 编辑时
                        if (rows && rows.length > 0) {
                            let row = rows[0]
                            planNedType = row.planNedType
                            planCorpCode = row.planCorpCode
                            planDeptCode = row.planDeptCode
                            planGoodType = row.planGoodType
                            isContract = row.planIsContract
                            planFactoryCodeTem = row.planFactoryCode
                            for (let i in selectedMMArr) {
                                let selectedMM = selectedMMArr[i]
                                if (planNedType != selectedMM.planNedType) {
                                    flag = false
                                    return
                                }
                                if (planCorpCode != selectedMM.planCorpCode) {
                                    flag = false
                                    return
                                }
                                if (planDeptCode != selectedMM.planDeptCode) {
                                    flag = false
                                    return
                                }
                                if (planGoodType != selectedMM.planGoodType) {
                                    flag = false
                                    return
                                }
                                if (isContract != selectedMM.planIsContract) {
                                    flag = false
                                    return
                                }
                                if (planFactoryCodeTem != selectedMM.planFactoryCode) {
                                    flag = false
                                    return
                                }
                            }
                        } else {
                            // 新增时
                            if (selectedMMArr.length > 1) {
                                planNedType = selectedMMArr[0].planNedType
                                planCorpCode = selectedMMArr[0].planCorpCode
                                planDeptCode = selectedMMArr[0].planDeptCode
                                planGoodType = selectedMMArr[0].planGoodType
                                isContract = selectedMMArr[0].planIsContract
                                planFactoryCodeTem = selectedMMArr[0].planFactoryCode
                                for (let i in selectedMMArr) {
                                    let selectedMM = selectedMMArr[i]
                                    if (planNedType != selectedMM.planNedType) {
                                        flag = false
                                        return
                                    }
                                    if (planCorpCode != selectedMM.planCorpCode) {
                                        flag = false
                                        return
                                    }
                                    if (planDeptCode != selectedMM.planDeptCode) {
                                        flag = false
                                        return
                                    }
                                    if (planGoodType != selectedMM.planGoodType) {
                                        flag = false
                                        return
                                    }
                                    if (isContract != selectedMM.planIsContract) {
                                        flag = false
                                        return
                                    }
                                    if (planFactoryCodeTem != selectedMM.planFactoryCode) {
                                        flag = false
                                        return
                                    }
                                }
                            }

                        }
                        return flag

                    }
                </script>

                </html>